/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GuiCamara.java
 *
 * Created on 05-nov-2011, 13:51:09
 */

package camara;

import java.awt.Color;
import java.awt.Container;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JRootPane;

/**
 *
 * @author ivan
 */
public class GuiCamara extends javax.swing.JFrame {
    // Icapa, donde se alojan las ventanas
    public Foscam miFoscam;
     public Reles losReles;
   
    /** Creates new form GuiCamara */
    Icon iconPlay;
    Icon iconStop;
    Icon iconApagada;
    Icon iconEncendida;

    public String[] laIP;
    public String[] elUser;
    public String[] elPass;
    public String[] elNombre;
    public int[] losFPS;
    public int[] elPulsador;
    private int numCamaras;

   
    public String[] relesTitulo;
    public String[] relesIP;
    private int numeroReles;
    
    /*
    public void setIconAlarma(int encendido){
        if (encendido==1)
            this.jAlarma.setIcon(iconEncendida);
        else
            this.jAlarma.setIcon(iconApagada);
    }
     */


    public GuiCamara() {
        int i;
        laIP = new String[4];
        elUser = new String[4];
        elPass = new String[4];
        elNombre = new String[4];
        losFPS = new int[4];
        elPulsador = new int[4];
        
        relesTitulo = new String[4];
        relesIP = new String[4];

        initComponents();
        this.jLabel1.setSize(640,480);
        this.numCamaras=this.LeeConfiguracion();
        if (numCamaras>0){
            miFoscam = new Foscam(this,laIP[0],elUser[0],elPass[0]);
            miFoscam.setFps(this.losFPS[0]);
            miFoscam.setCapture(true);
            miFoscam.start();
            this.jTitulo.setText(this.elNombre[0]);
            iconPlay= new ImageIcon("play.png");
            iconStop=new ImageIcon("stop.png");
            iconApagada = new ImageIcon("fuegoApagado.png");
            iconEncendida = new ImageIcon("fuegoEncendido.png");
            this.jPlay.setIcon(iconStop);
        }
        for (i=0;i<this.numCamaras;i++){
            this.jListaCamaras.addItem(this.elNombre[i]);
        }
        this.numeroReles=this.LeeConfiguracionReles();
        if (this.numeroReles>0){
            losReles = new Reles();
            losReles.RelesSetIp(relesIP[0]);
            losReles.setVisible(false);
            losReles.setTitle(relesTitulo[0]);
        }else{
            this.jBotonReles.setEnabled(false);
        }
        
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jSubir = new javax.swing.JButton();
        jBajar = new javax.swing.JButton();
        jIzquierda = new javax.swing.JButton();
        jDerecha = new javax.swing.JButton();
        jPlay = new javax.swing.JButton();
        jTitulo = new javax.swing.JLabel();
        jListaCamaras = new javax.swing.JComboBox();
        jBotonReles = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("WebCamControl 1.0");
        setBackground(new java.awt.Color(0, 0, 0));
        setBounds(new java.awt.Rectangle(20, 20, 640, 480));
        setForeground(java.awt.Color.white);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        jLabel1.setBackground(new java.awt.Color(1, 1, 1));
        jLabel1.setForeground(new java.awt.Color(62, 16, 252));
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel1.setVerticalAlignment(javax.swing.SwingConstants.TOP);
        jLabel1.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(14, 1, 155), 2, true));
        jLabel1.setDoubleBuffered(true);
        jLabel1.setOpaque(true);
        jLabel1.setPreferredSize(new java.awt.Dimension(640, 480));

        jSubir.setForeground(new java.awt.Color(0, 41, 255));
        jSubir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/camara/abajo.png"))); // NOI18N
        jSubir.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jSubirMouseClicked(evt);
            }
        });

        jBajar.setForeground(new java.awt.Color(0, 41, 255));
        jBajar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/camara/arriba.png"))); // NOI18N
        jBajar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jBajarMouseClicked(evt);
            }
        });

        jIzquierda.setForeground(new java.awt.Color(0, 41, 255));
        jIzquierda.setIcon(new javax.swing.ImageIcon(getClass().getResource("/camara/derecha.png"))); // NOI18N
        jIzquierda.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jIzquierdaMouseClicked(evt);
            }
        });

        jDerecha.setForeground(new java.awt.Color(0, 41, 255));
        jDerecha.setIcon(new javax.swing.ImageIcon(getClass().getResource("/camara/izquieda.png"))); // NOI18N
        jDerecha.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jDerechaMouseClicked(evt);
            }
        });

        jPlay.setIcon(new javax.swing.ImageIcon(getClass().getResource("/camara/stop.png"))); // NOI18N
        jPlay.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jPlayMouseClicked(evt);
            }
        });
        jPlay.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jPlayActionPerformed(evt);
            }
        });

        jTitulo.setForeground(new java.awt.Color(61, 16, 175));
        jTitulo.setText("jLabel2");

        jListaCamaras.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jListaCamarasItemStateChanged(evt);
            }
        });

        jBotonReles.setText("Relés");
        jBotonReles.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jBotonRelesMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(50, 50, 50)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jTitulo)
                        .addGap(27, 27, 27)
                        .addComponent(jListaCamaras, javax.swing.GroupLayout.PREFERRED_SIZE, 190, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jBotonReles, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPlay, javax.swing.GroupLayout.Alignment.LEADING, 0, 0, Short.MAX_VALUE)
                    .addComponent(jDerecha, javax.swing.GroupLayout.Alignment.LEADING, 0, 0, Short.MAX_VALUE)
                    .addComponent(jIzquierda, javax.swing.GroupLayout.Alignment.LEADING, 0, 0, Short.MAX_VALUE)
                    .addComponent(jSubir, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jBajar, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 480, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jBajar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jSubir)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jIzquierda)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jDerecha)
                        .addGap(18, 18, 18)
                        .addComponent(jPlay)
                        .addGap(19, 19, 19)
                        .addComponent(jBotonReles)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTitulo)
                    .addComponent(jListaCamaras, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(47, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        // TODO add your handling code here:
        //this.setSize(640,480);
        Container con=this.getContentPane();
        con.setBackground(Color.BLACK);
        this.setBackground(Color.BLACK);
        
        this.setResizable(false);
    }//GEN-LAST:event_formWindowOpened

    private void jBajarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jBajarMouseClicked
        miFoscam.setBaja();
    }//GEN-LAST:event_jBajarMouseClicked

    private void jSubirMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jSubirMouseClicked
        // TODO add your handling code here:
        miFoscam.setSube();
    }//GEN-LAST:event_jSubirMouseClicked

    private void jIzquierdaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jIzquierdaMouseClicked
        // TODO add your handling code here:
        miFoscam.setIzquierda();
    }//GEN-LAST:event_jIzquierdaMouseClicked

    private void jDerechaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jDerechaMouseClicked
        // TODO add your handling code here:
        miFoscam.setDerecha();
    }//GEN-LAST:event_jDerechaMouseClicked

    private void jPlayMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jPlayMouseClicked
        // TODO add your handling code here:
        if (miFoscam.getCapture()==true){
            miFoscam.setCapture(false);
            this.jPlay.setIcon(iconPlay);
        }
        else{
            this.jPlay.setIcon(iconStop);
            miFoscam.setCapture(true);
        }
    }//GEN-LAST:event_jPlayMouseClicked

    private void jListaCamarasItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jListaCamarasItemStateChanged
        // TODO add your handling code here:
        int num;
        num=this.jListaCamaras.getSelectedIndex();

        miFoscam.setIP(this.laIP[num]);
        miFoscam.setUsuario(this.elUser[num]);
        miFoscam.setPass(this.elPass[num]);
        miFoscam.setModoPulsador(this.elPulsador[num]);
        this.jTitulo.setText(this.elNombre[num]);
        /*
        if (elPulsador[num]==1){
            this.jPulsador.setSelected(true);
            this.jRele.setText("Pulsar");
        }
        else
            this.jPulsador.setSelected(false);
         */
        

    }//GEN-LAST:event_jListaCamarasItemStateChanged

    private void jPlayActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jPlayActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jPlayActionPerformed

    private void jBotonRelesMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jBotonRelesMouseClicked
        if (losReles.isVisible()==false)
            losReles.setVisible(true);
        else
            losReles.setVisible(false);
    }//GEN-LAST:event_jBotonRelesMouseClicked
    private int LeeConfiguracionReles(){
        String[] aux;
        String linea;

        int num=0;
        aux = new String[2];
        try {

            BufferedReader conf = new BufferedReader(new FileReader("reles.txt"));

            try {
                do{
                    linea=conf.readLine();
                    if (linea==null) break; // Si no hay nada salimos
                    if (linea.length()<4) break;    // Si hay mierda en la linea
                    if (linea.charAt(0)=='['){ // Es el nombre de la camara
                        // Perfecto es el nombre de la camara
                        
                        this.relesTitulo[num]=linea.substring(1,linea.length()-1);
                        continue;
                    }
                    aux=linea.split("=");
                    if (aux[0].compareTo("IP")==0){
                        this.relesIP[num]=aux[1];
                        num=num+1;
                        if (num>4) break;
                        
                    }
                }while(linea!=null);
                conf.close();
                if (num>0)
                    return num;
                else
                    return 0;

            } catch (IOException ex) {
                Logger.getLogger(GuiCamara.class.getName()).log(Level.SEVERE, null, ex);
            }


        } catch (FileNotFoundException ex) {
            Logger.getLogger(GuiCamara.class.getName()).log(Level.SEVERE, null, ex);
        }

       
        return 0;
    }
    private int LeeConfiguracion(){
        String[] aux;
        String linea;

        int num=0;
        aux = new String[2];
        try {

            BufferedReader conf = new BufferedReader(new FileReader("camaras.txt"));

            try {
                do{
                    linea=conf.readLine();
                    if (linea==null) break; // Si no hay nada salimos
                    if (linea.length()<4) break;    // Si hay mierda en la linea
                    if (linea.charAt(0)=='['){ // Es el nombre de la camara
                        // Perfecto es el nombre de la camara
                        elNombre[num]=linea.substring(1,linea.length()-1);
                        continue;
                    }
                    aux=linea.split("=");
                    if (aux[0].compareTo("IP")==0){
                        laIP[num]=aux[1];
                        continue;
                    }
                    if (aux[0].compareTo("USER")==0){
                        elUser[num]=aux[1];
                        continue;
                    }
                    if (aux[0].compareTo("PASS")==0){
                        if (linea.length()==aux[0].length()+1)
                            elPass[num]="";
                        else
                            elPass[num]=aux[1];

                        continue;
                        
                    }
                    if (aux[0].compareTo("FPS")==0){
                        if (linea.length()==aux[0].length()+1)
                            losFPS[num]=2;
                        else
                            losFPS[num]=Integer.parseInt(aux[1]);
                        continue;
                        
                    }
                    if (aux[0].compareTo("PULSADOR")==0){
                        if (linea.length()==aux[0].length()+1)
                            elPulsador[num]=1;
                        else{                        
                            elPulsador[num]=Integer.parseInt(aux[1]);
                        }
                        num=num+1;
                        if (num>4) break;
                    }

                }while(linea!=null);
                conf.close();
                if (num>0)
                    return num;
                else
                    return 0;

            } catch (IOException ex) {
                Logger.getLogger(GuiCamara.class.getName()).log(Level.SEVERE, null, ex);
            }


        } catch (FileNotFoundException ex) {
            Logger.getLogger(GuiCamara.class.getName()).log(Level.SEVERE, null, ex);
        }

        return 0;
    }


    public Object getTv(){
        return this.jLabel1;
    }
    public void setImagenLabel1(BufferedImage laImagen){
        this.jLabel1.setIcon(new ImageIcon(laImagen));
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new GuiCamara().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBajar;
    private javax.swing.JButton jBotonReles;
    private javax.swing.JButton jDerecha;
    private javax.swing.JButton jIzquierda;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JComboBox jListaCamaras;
    private javax.swing.JButton jPlay;
    private javax.swing.JButton jSubir;
    private javax.swing.JLabel jTitulo;
    // End of variables declaration//GEN-END:variables

}
